package com.peanut.controller;

import com.mvc.annotation.RequestBody;
import com.mvc.annotation.RequestMapping;
import com.mvc.annotation.ResponseBody;
import com.peanut.constants.Constants;
import com.peanut.entity.AdminUser;
import com.peanut.entity.JsonResult;
import com.peanut.entity.dto.MyInfo;
import com.peanut.entity.dto.MyPassword;
import com.peanut.service.AdminUserService;
import com.spring.annotation.Autowired;
import com.spring.annotation.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
public class MyInfoController {
	@Autowired
	private AdminUserService adminUserService;

	/**
	 * 查询自己的个人信息，昵称和头像
	 */
	@RequestMapping(value = "/my/query.do")
	@ResponseBody
	public JsonResult queryMyInfo(HttpServletRequest request){
		HttpSession session = request.getSession();
		AdminUser loginUser = (AdminUser) session.getAttribute(Constants.LOGIN_USER);
		Integer id = loginUser.getId();
		
		MyInfo myInfo = adminUserService.queryMyInfo(id);
		return JsonResult.success(myInfo);
	}
	
	@RequestMapping(value = "/my/update.do")
	@ResponseBody
	public JsonResult updateMyInfo(HttpServletRequest request,@RequestBody MyInfo myInfo){
		HttpSession session = request.getSession();
		AdminUser loginUser = (AdminUser) session.getAttribute(Constants.LOGIN_USER);
		Integer id = loginUser.getId();
		
		if(adminUserService.updateMyInfo(myInfo,id)){
			return JsonResult.success();
		}
		return JsonResult.fail();
	}
	
	@RequestMapping(value = "/my/modifyMyPassword.do")
	@ResponseBody
	public JsonResult modifyMyPassword(HttpServletRequest request,@RequestBody MyPassword myPassword){
		HttpSession session = request.getSession();
		AdminUser loginUser = (AdminUser) session.getAttribute(Constants.LOGIN_USER);
		Integer id = loginUser.getId();
		
		if(adminUserService.modifyMyPassword(myPassword,id)){
			return JsonResult.success();
		}
		return JsonResult.fail("确认下原密码输入正确");
	}
}
